package patrol.mobile.com.mobilepatrol.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.dl7.recycler.adapter.BaseQuickAdapter;

import java.util.List;

import butterknife.BindView;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;

public abstract class CommonListActivity extends BaseActivity {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    protected BaseQuickAdapter mAdapter;
    public static final int ROWS = 10;
    @BindView(R.id.fl_content)
    public FrameLayout flContent;
    @BindView(R.id.titleAddIcon)
    public ImageView titleAddIcon;

    @Override
    public void initTitle() {
        super.initTitle();
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(CommonListActivity.this, true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText(initTitleName());
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_common_list;
    }

    @Override
    public void initData() {
        super.initData();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = initAdapter();
        recyclerView.setAdapter(mAdapter);
        configAdapter();
    }

    protected abstract BaseQuickAdapter initAdapter();

    protected abstract String initTitleName();

    public void configAdapter() {

    }

    @Override
    public void setListener() {

    }

    public void setData(int page, boolean isRefresh, List data, int rows) {
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapter.updateItems(data);
        } else {
            int totalPage;
            if ((int) Math.ceil(rows / ROWS) == 0) {
                totalPage = 1;
            } else {
                totalPage = (int) Math.ceil(rows / ROWS);
            }
            if (page == totalPage) {
                if (page == 1) {
                    mAdapter.updateItems(data);
                    mAdapter.enableLoadMore(false);//加载loading gone掉
                } else {
                    mAdapter.addItems(data);
                    mAdapter.noMoreData();
                }
            } else {
                mAdapter.addItems(data);
//                mAdapter.enableLoadMore(false);//加载loading gone掉
                mAdapter.loadComplete();//本次加载完成
            }
        }
    }
}
